package com.sx.page.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sx.page.fegin.ProductFegin;
import common.pojo.Products;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/page")
public class PageController {

@Autowired
 private ProductFegin productFegin;


    @GetMapping("/findById/{id}")
    public Products getProduct(@PathVariable long id) {
         return productFegin.findById(id);

    }

    @GetMapping("/sercicepost")
    public String getServicePost() {
        // 使用服务名而不是IP地址，让RestTemplate的负载均衡功能生效
        String port = productFegin.getPort();
        return port;
    }

    /**
     * 模拟服务超时，熔断处理
     *
     * @return
     */
    @HystrixCommand(
            fallbackMethod = "getServicePost2", // 指定服务降级处理方法,实际开发中每个方法一个线程池
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"),//并非线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20")//默认线程队列是-1，-1表示无界队列
                    //线程池

            },
            //设置请求的超时时间
            commandProperties = {
                    //设置超时时间
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    //统计窗口时间的设置
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计窗口内的最小请求数
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计窗口内错误请求阈值的设置  50%
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //自我修复的活动窗口时间
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            }
    )
    @GetMapping("/sercicepost2")
    public String getServicePost2() {
        String port = productFegin.getPort();
        return port;
    }

    /**
     * 服务降级处理方法
     */
    @HystrixCommand(
            //设置请求的超时时间
            commandProperties = {
                    //设置超时时间
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    //统计窗口时间的设置
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计窗口内的最小请求数
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计窗口内错误请求阈值的设置  50%
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //自我修复的活动窗口时间
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            },//设置回退方法
            fallbackMethod = "fallBack"

    )

    @GetMapping("/sercicepost3")
    public String getServicePost3() {
        // 使用服务名而不是IP地址，让RestTemplate的负载均衡功能生效
        String port = productFegin.getPort();
        return port;
    }

    /**
     * 定义一个回退方法，当请求熔断时
     * 1、方法的新参数必须与原方法一致
     * 2、方法返回值必须与原方法一致
     */

    public String fallBack() {
        return "服务已熔断";
    }


}